//
//  DaoViewController.swift
//  TXiOSDemoApp
//
//  Created by xicheng on 16/11/27.
//  Copyright © 2016年 xicheng. All rights reserved.
//

import UIKit

class DaoViewController:TXBaseViewController{

  fileprivate var tableView:UITableView! = nil
  
  fileprivate var adapter:DaoAdapter! = nil
  
  
  fileprivate lazy var dateFormatter:DateFormatter = {
    let dateFormat = DateFormatter()
    dateFormat.dateFormat = "YYYY_MM_dd-HH_mm_ss"
    return dateFormat
  }()
  
  override func initView() {
    self.naviBar.title = "DaoViewController"
    
    self.tableView = UITableView(frame: self.view.bounds)
    self.tableView.contentInset = UIEdgeInsetsMake(kNaviHeight, 0, 0, 0)
    self.view.addSubview(self.tableView)
    
    self.adapter = DaoAdapter(vc: self)
    self.adapter.setupTableView(tableView: self.tableView)
    
    self.tableView.separatorStyle = .singleLine
  }
  

  override func initData() {
    if  let dataList = UserModel.findAll(WithDescFlag: true) as? [UserModel] {
      self.adapter.refreshCount(listData: dataList)
    }
  }
  
  
}

extension DaoViewController {
  
  override func needShowProgress() -> Bool {
    return false
  }

  override func rightBarButton() -> UIButton? {
    
    let btn = UIButton(frame:CGRect(x: kMainScreenWidth - 100, y: 20, width: 100, height: 44))
    btn.setTitle("插入Cell", for: .normal)
    btn.setTitleColor(UIColor.black, for: .normal)
    btn.fixSingleClickListener(0.6, ClickListener.init(block: { 
      [unowned self] in
      
      let currentTime = dateFormat.string(from: Date())
      let model = UserModel()
      model.name = "测试Name:\(currentTime)"
      model.address = "测试地址:\(currentTime)"
      let insertFlag = model.save()
      if insertFlag {
        self.adapter.dataList = UserModel.findAll(WithDescFlag: true) as? [UserModel]
        self.tableView.beginUpdates()
        self.tableView.insertRows(at: [IndexPath.init(row: 0, section: 0)], with: UITableViewRowAnimation.automatic)
        self.tableView.endUpdates()
      }else {
        self.showToast("插入失败")
      }
      
    }))
    return btn
  }
  
}


class UserModel:TaurusDBModel {
  
  internal var name:String?

  internal var address:String?
  
  override class func getPrimarayKey() -> String {
    return "name"
  }
  
}

class DaoAdapter:BaseAdapter<UserModel,UITableViewCell,DaoViewController>{
  override func setupCell(_ tableView: UITableView, cell: UITableViewCell, model: UserModel, indexPath: IndexPath) {
    
      cell.textLabel?.text = model.name
      cell.detailTextLabel?.text = model.address
  }
  
  override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 54
  }
  
  override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    return true
  }

  override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
    return .delete
  }
  
  override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    let deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.destructive, title: "删除") { [unowned self](action, index) in
      guard let tableView = self.tableView , let model = self.dataList?[indexPath.row] else {
        return
      }
      tableView.setEditing(false, animated: true)
      let deleteFlag = model.delete()
      
      if deleteFlag {
        
        self.dataList = UserModel.findAll(WithDescFlag: true) as? [UserModel]
        tableView.beginUpdates()
        tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
        tableView.endUpdates()
      }else {
        self.targetVC?.showToast("删除失败")
      }
      
      
    }
    deleteAction.backgroundColor = UIColor.red
    return [deleteAction]
  }
  
}
